const app = getApp()
const db = wx.cloud.database({
  env:'xiaochengxu-2gcm02cv74f2b2b8'
})
const userList = db.collection("userList")

Page({
  data:{
        name:'',
        phone:'',
        password:'',
  },
  //注册
  register() {
    let that = this;
    let {name,phone,password} = this.data
    if(name.trim().length===0 && phone.trim().length===0 && password.trim().length===0) {
      wx.showToast({
        title: '您信息没输入完整',
        icon:'none'
      })
    } else {
      if(phone.trim().length<6 && password.trim().length<6) {
        wx.showToast({
          title: '账号和密码不能少于6位数',
          icon:'none'
        })
      }  else {
    //查询用户是否已经注册
    userList.where({
      // _openid: app.globalData.openid // 填入当前用户 openid
      phone:phone
    }).get({
      success: function (res) {
        console.log(res);
        if(res.data.length===0) {
          userList.add({
            data:{
              name:name,
              phone:phone,
              password:password
            },
            success(res) {
                wx.showToast({
                  title: '注册成功，跳转登录页面',
                  icon:'none',
                   duration:2000
                })
                setTimeout(function(){
                  wx.navigateTo({
                    url: '/pages/login/login',
                  })
                },2000)
            }
          })    
      
        } else {
          wx.showToast({
            title: '该账号已注册',
            icon:'none',
             duration:2000
          })
        }
      }
    })
  }
    }

  },

  InputName: function (e) {
    this.setData({
    name: e.detail.value
    })
    },
    InputPhone: function (e) {
      this.setData({
      phone: e.detail.value
      })
      },
      InputPassword: function (e) {
        this.setData({
        password: e.detail.value
        })
        },
})